package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
  ImageView imageView;

  public ImageAsyncTask(ImageView imageView) {
    this.imageView = imageView;
  }

  @Override protected Bitmap doInBackground(String... urls) {
    String imageUrl = urls[0];
    Bitmap bitmap = null;
    try {
      System.out.println("开始运行");
      URL url=new URL(imageUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setDoInput(true);
      connection.connect();
      InputStream inputStream=connection.getInputStream();
      bitmap = BitmapFactory.decodeStream(inputStream);
      inputStream.close();
      System.out.println("完事了");

    } catch (Exception e) {
      Log.e("Error", e.getMessage());
      e.printStackTrace();

    }
    return bitmap;
  }

  @Override protected void onPostExecute(Bitmap bitmap) {
    imageView.setImageBitmap(bitmap);
    System.out.println("加载图片了");

  }
}
